import { translate } from "../../utils/api"
const app = getApp()

Page({
  data: {
    "query":'',
    "hideClearIcon":true,
    "result":[],
    "curLang":{}
  },
  onLoad:function(){
  },
  onShow:function(){
    if(this.data.curLang!==app.globalData.curLang){
      this.setData({curLang:app.globalData.curLang})
      this.onComfirm()
    }
  },
  onInput:function(e){
    // 输入的时候有关闭按钮
    this.setData({'query':e.detail.value.trim()})
    if(this.data.query.length>0){
      this.setData({'hideClearIcon':false})
    }else{
      this.setData({'hideClearIcon':true})
    }
  },

  onTapClose:function(){
    //点击关闭按钮
    this.setData({'hideClearIcon':true,'query':''})
  },
  onComfirm:function(){
    if(!this.data.query)return
    translate (this.data.query,{from : 'auto', to : this.data.curLang.lang}).then(res=>{
      this.setData({'result':res.trans_result})
      let r1=this.data.result.map(n=>{return n.dst})
      this.setData({'result':r1})

      let history=wx.getStorageSync('history')||[]
      history.unshift({query:this.data.query,result:this.data.result})
      history.length=history.length>20?20:history.length
      let x=wx.setStorageSync('history', history)
    })
  } 
})